<?php
/**
 * Object Main_Home_slider
 * @author SillySmart
 * @copyright SillySmart
 * @package Mvc.Models.Objects
 * @see Sls.Models.Core.SLS_FrontModel
 * @since 1.0
 */
class Main_Home_slider extends SLS_FrontModel
{
	/**
	 * Class variables
	 */
	protected $__home_slider_id;
	protected $__pk_lang;
	protected $__home_slider_title;
	protected $__home_slider_title_more;
	protected $__home_slider_img;
	protected $_table = "home_slider";
	protected $_db = "main";
	protected $_primaryKey = "home_slider_id";
	protected $_fks = array();
	public $_typeErrors = array();

	/**
	 * Constructor Main_Home_slider
	 * @author SillySmart
	 * @copyright SillySmart
	 * @param bool $mutlilanguage true if multilanguage content, else false
	 */
	public function __construct($multilanguage=true)
	{
		parent::__construct($multilanguage);
		$this->buildDefaultValues();
	}

	/**
	 * Build default values for specific columns
	 * @author SillySmart
	 * @copyright SillySmart
	 */
	public function buildDefaultValues()
	{
	}

	/**
	 * Set the value of home_slider_title
	 * Errors can be catched with the public variable $this->_typeErrors['home_slider_title']
	 * @author SillySmart
	 * @copyright SillySmart
	 * @param string $value
	 * @return bool true if updated, false if not
	 */
	public function setHomeSliderTitle($value)
	{
		if (empty($value))
		{
			$this->__set('home_slider_title', $value);
			return true;
		}

		if ($this->__home_slider_title == $value)
			return true;

		if (strlen($value) > 127)
		{
			$this->_typeErrors['home_slider_title'] = "E_LENGTH";
			return false;
		}

		$this->__set('home_slider_title', $value);
		return true;
	}

	/**
	 * Set the value of home_slider_title_more
	 * Errors can be catched with the public variable $this->_typeErrors['home_slider_title_more']
	 * @author SillySmart
	 * @copyright SillySmart
	 * @param string $value
	 * @return bool true if updated, false if not
	 */
	public function setHomeSliderTitleMore($value)
	{
		if (empty($value))
		{
			$this->__set('home_slider_title_more', $value);
			return true;
		}

		if ($this->__home_slider_title_more == $value)
			return true;

		if (strlen($value) > 127)
		{
			$this->_typeErrors['home_slider_title_more'] = "E_LENGTH";
			return false;
		}

		$this->__set('home_slider_title_more', $value);
		return true;
	}

	/**
	 * Set the value of home_slider_img
	 * Errors can be catched with the public variable $this->_typeErrors['home_slider_img']
	 * @author SillySmart
	 * @copyright SillySmart
	 * @param string $value
	 * @return bool true if updated, false if not
	 */
	public function setHomeSliderImg($value)
	{
		if (!empty($this->__home_slider_img))
		{
			$this->deleteFiles(array("home_slider_img"));
			$this->__home_slider_img = "__Uploads/__Deprecated/".$this->__home_slider_img;
			$this->save();
		}

		if (is_array($value))
		{
			if ($value["error"] == 1 || $value["error"] == 2)
			{
				$this->_typeErrors['home_slider_img'] = "E_SIZE";
				return false;
			}
			else
			{
				try {
					if (!file_exists($this->_generic->getPathConfig("files").$this->_table))
						mkdir($this->_generic->getPathConfig("files").$this->_table,0755);

					$tmpName = SLS_String::substrBeforeLastDelimiter($value["tmp_name"],"/")."/".$value["name"];
					$token = substr(md5(time().substr(sha1(microtime()),0,rand(5,12))),mt_rand(1,20),10);
					$fileName = SLS_String::sanitize(SLS_String::substrBeforeLastDelimiter($value["name"],"."),"_")."_".$token;
					@rename($value["tmp_name"],$tmpName);
					$extension = pathinfo($tmpName, PATHINFO_EXTENSION);

					// Check img
					$img = new SLS_Image($tmpName);
					if ($img->getParam("existed"))
					{
						$img->transform("","",$this->_generic->getPathConfig("files").$this->_table."/".$fileName.".".$extension,$extension);

						// Check thumbs
						$xmlType = new SLS_XMLToolbox(file_get_contents($this->_generic->getPathConfig("configSls")."/types.xml"));
						$result = array_shift($xmlType->getTagsAttribute("//sls_configs/entry[@table='".$this->getDatabase()."_".$this->getTable()."' and @column='".home_slider_img."' and @type='file_img']","thumbs"));
						$thumbs = unserialize(str_replace("||#||",'"',$result["attribute"]));
						if (!empty($thumbs))
						{
							for($i=0 ; $i<$count=count($thumbs) ; $i++)
							{
								$img->transform($thumbs[$i]["width"],$thumbs[$i]["height"],$this->_generic->getPathConfig("files").$this->_table."/".$fileName.$thumbs[$i]["suffix"].".".$extension,$extension);
							}
						}
					}
					else
					{
						$this->_typeErrors['home_slider_img'] = "E_TYPE";
						return false;
					}
					$value = $this->_table."/".$fileName.".".$extension;
				}
				catch (Exception $e) {
					$this->_typeErrors['home_slider_img'] = "E_TYPE";
					return false;
				}
			}
		}

		if ($value === "")
		{
			$this->_typeErrors['home_slider_img'] = "E_EMPTY";
			return false;
		}

		if (is_null($value))
		{
			$this->_typeErrors['home_slider_img'] = "E_NULL";
			return false;
		}

		if ($this->__home_slider_img == $value)
			return true;

		if (strlen($value) > 255)
		{
			$this->_typeErrors['home_slider_img'] = "E_LENGTH";
			return false;
		}

		$this->__set('home_slider_img', $value);
		return true;
	}

}
?>